/**
 * Created with JetBrains WebStorm.
 * User: zhoums
 * Date: 14-1-17
 * Time: 下午1:00
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    /*
    * fn:鼠标指向提示
    * @e：触发事件对象
    * */
    $(document).delegate("[cantit]","mouseenter",function(e){var msg = $(this).attr("cantit");showTxtTip(e, msg);})
    $(document).delegate("[cantit]","mouseleave",function(e){ $("#tip").remove();})


})

function showTxtTip(e, msg) {
    var x = e.pageX, y = e.pageY;
    var $nav = $('<div id="tip"></div>').text(msg);
    $nav.css({position: "absolute", top: y - 20 + "px", left: x + 10 + "px", border: "1px solid #878585","color":"#9f9d9d", "background-color": "#fff", "z-index": "500", "border-radius": "3px", "padding": "0 5px"});
    $("body").append($nav);
}


function formatParam(str){
    str=str.replace(/=/g,":");
    var aParam=$.toJSON(str.split("&"));

   return aParam;
}


//设置cookie值
function setCookie(name,value,hours,path){
    var name = escape(name);
    var value = escape(value);
    var expires = new Date();
    expires.setTime(expires.getTime() + hours*3600000);
    path = path == "" ? "" : ";path=" + path;
    _expires = (typeof hours) =="string"? "" : ";expires=" + expires.toUTCString();
    document.cookie = name + "=" + value + _expires + path;
}
//获取cookie值
function getCookieValue(name){
    var name = escape(name);
    //读cookie属性，这将返回文档的所有cookie
    var allcookies = document.cookie;
    //查找名为name的cookie的开始位置
    name += "=";
    var pos = allcookies.indexOf(name);
    //如果找到了具有该名字的cookie，那么提取并使用它的值
    if (pos != -1){                                             //如果pos值为-1则说明搜索"version="失败
        var start = pos + name.length;                  //cookie值开始的位置
        var end = allcookies.indexOf(";",start);        //从cookie值开始的位置起搜索第一个";"的位置,即cookie值结尾的位置
        if (end == -1) end = allcookies.length;        //如果end值为-1说明cookie列表里只有一个cookie
        var value = allcookies.substring(start,end);  //提取cookie的值
        return unescape(value);                           //对它解码
    }
    else return "";                                             //搜索失败，返回空字符串
}
//删除cookie
function deleteCookie(name,path){
    var name = escape(name);
    var expires = new Date(0);
    path = path == "" ? "" : ";path=" + path;
    document.cookie = name + "="+ ";expires=" + expires.toUTCString() + path;
}
/*
* fn alarm : 表单验证不通过：背景色变成红色提醒
* @el{$ obj} 支持数组
* */
function alarm(el){
      if(el.length&&el.length==1){
          el.addClass("error");

      }else if(el.length&&el.length>1){
          $.each(el,function(i,item){
              item.addClass("error");
          })

      }
}

function cancelAlarm(el){
    if(el.length&&el.length==1){
        el.removeClass("error");

    }else if(el.length&&el.length>1){
        $.each(el,function(i,item){
            item.removeClass("error");
        })

    }
}

function toggleAlarm(el){
    var fn='alarm('+el+')',fn_can='cancelAlarm('+el+')';
    console.log(el);
    alarm(el);
    setTimeout("cancelAlarm("+el+")",500);
    setTimeout(fn,1000);
    setTimeout(fn_can,1500);
}
   /*
   *Format("yyyy年MM月dd日");
    Format("MM/dd/yyyy");
    Format("yyyyMMdd");
    Format("yyyy-MM-dd hh:mm:ss");
   */
Date.prototype.format = function(format){
    var o = {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(), //day
        "h+" : this.getHours(), //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds() //millisecond
    }

    if(/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }

    for(var k in o) {
        if(new RegExp("("+ k +")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
        }
    }
    return format;
}


